import { Component, OnInit, Input } from '@angular/core';
import { BaselineService } from 'src/app/core/service/baseline.service';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { AddBaselineComponent } from '../../../data-center-modal/add-baseline/add-baseline.component';

@Component({
  selector: 'app-baseline-list',
  templateUrl: './baseline-list.component.html',
  styleUrls: ['./baseline-list.component.scss']
})
export class BaselineListComponent implements OnInit {
  @Input() containerUuid = '';

  data = [];
  pageIndex = 1;
  pageSize = 10;
  loading = true;
  total = 0;

  constructor(
    private baselineService: BaselineService,
    private message: NzMessageService,
    private modalService: NzModalService,
  ) { }

  getBaselineList(reset: boolean = false): void {
    if (reset) {
      this.pageIndex = 1;
    }
    this.loading = true;

    this.baselineService
      .getBaselineList(this.containerUuid)
      .subscribe((BaselineData) => {
        this.loading = false;
        this.total = BaselineData.total;
        this.data = BaselineData.data;
        console.log('BaselineData', BaselineData);
      });
  }

  createComponentModal(): void {
    const modal = this.modalService.create({
      nzTitle: '添加组件',
      nzContent: AddBaselineComponent,
      nzComponentParams: {
        containerUuid: this.containerUuid
      },
      nzFooter: null,
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getBaselineList();
    });
  }

  deleteBaseline(name: string, number: string) {
    this.baselineService.deleteBaseline(name, number).subscribe((baselineData) => {
      if (baselineData.status) {
        this.message.success('删除成功!', {
          nzDuration: 10000
        });
        this.getBaselineList();
      } else {
        this.message.error('添加失败!请检查后重新填写', {
          nzDuration: 10000
        });
      }
    });
  }

  ngOnInit() {
    console.log(this.containerUuid);
    this.getBaselineList();
  }

}
